//
//  QDFacilityViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/1.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 车站设施
final class QDFacilityViewController: QDOriginPageViewController {

    override var titles: [String] {
        get {
            return ["基础设施","无障碍设施","生活便利服务"]
        }
    }
    
    override var pageControllers: [UIViewController] {
        get {
            return [QDFacilityTableViewController(),QDFacilityTableViewController(type: .barrier),QDFacilityTableViewController(type: .life)]
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        navigationItem.title = "车站设施"
    }
    
    
    override func configSegment() {
        pageController.segmentBar.borderMarginInset.left = 20
        pageController.segmentBar.borderMarginInset.right = 20
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}



/// 车站设施内嵌控制器的cell
final class QDFacilityTableViewCell: NWTableViewCell {
    
    /// 图像
    let headerImageView = UIImageView()
    /// 显示标题
    let titleLabel = UILabel()
    /// 详细描述
    let subtitleLabel = UILabel()
    
    override func buildView() {
        super.buildView()
        
        headerImageView.contentMode = .scaleAspectFill
        headerImageView.clipsToBounds = true
        headerImageView.backgroundColor = .white
        
        titleLabel.textColor = QDColors.theme
        titleLabel.font = Font.regular.font(size: 15)
        titleLabel.text = "换乘提示"
        
        subtitleLabel.textColor = QDColors.black
        subtitleLabel.font = Font.regular.font(size: 15)
        subtitleLabel.numberOfLines = 3
        subtitleLabel.text = "用于引导换乘特殊路线的提示,用于引导换乘特殊路线的提示,用于引导换乘特殊路线的提示乘特殊路线的提示乘特殊路线的提示"
        
        contentView.addSubview(headerImageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(subtitleLabel)
        
        headerImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(80)
            make.height.equalTo(80 / 0.75)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headerImageView.snp.right).offset(10)
            make.top.equalTo(headerImageView)
            make.right.equalToSuperview().inset(10)
        }
        
        subtitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.left)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.right.equalToSuperview().inset(10)
        }
        
        bottomLineInset.right = 10
        bottomLineInset.left = 10
        bottomLineHeight = 0.5
        bottomLineView.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
    }
}


/// 车站设施内嵌控制器
final class QDFacilityTableViewController: NWTableViewController {
    
    enum QDFacilityType: String {
        /// 基础设施
        case base = "40"
        /// 障碍相关设施
        case barrier = "34"
        /// 生活便利服务
        case life = "41"
    }
    
    /// 类型
    var type = QDFacilityType.base
    /// 数据源
    private var textList = [QDTextListModel]()
    
    
    convenience init(type: QDFacilityType = .base){
        self.init()
        self.type = type
    }
    
    
    override func registerTableViewCellsAndReusableView() {
        tableView.rowHeight = 138
        tableView.register(QDFacilityTableViewCell.self, forCellReuseIdentifier: "cell")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //开始请求
        headerRefreshBeginHandler()
    }
    
    
    override func headerRefreshBeginHandler() {
        currentPage = 0
        tableView.mj_footer.resetNoMoreData()
        requestTextList()
    }
    
    
    override func footerRefreshBeginHandler() {
        requestTextList()
    }
}

extension QDFacilityTableViewController {
    
    /// 请求数据
    private func requestTextList(){
        
        QDNetworkingManager.requestConvenienceMessage(type: QDNetworkingManager.ConvenienceType(rawValue: type.rawValue) ?? .base, page: currentPage, complete: { (datas) in
            
            self.endHeaderRefreshing()
            self.endFooterRefreshing()
            
            if self.currentPage == 0 {
                self.textList.removeAll()
            }
            
            self.textList += datas
            
            //刷新
            self.tableView.reloadData()
            self.currentPage += 1
            
            if datas.count < 10 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }

        }) { (msg, errorType) in
            
           self.endHeaderRefreshing()
           self.endFooterRefreshing()
           self.view.showTextHUD(text: msg)
        }
    }
}




extension QDFacilityTableViewController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return textList.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        if let facilityCell = cell as? QDFacilityTableViewCell {
            let textRow = textList[indexPath.row]
            
            //进行设置
            facilityCell.titleLabel.text = textRow.title
            facilityCell.subtitleLabel.text = textRow.content
            facilityCell.headerImageView.kf.setImage(with: URL(string: textRow.imageUrl))
        }
        
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let textModel = textList[indexPath.row]
        pushToShareWebViewController(url: textModel.webUrl, shareTitle: textModel.title, title: "详情")//跳转web
    }
}




